package com.kuang.conroller;

import com.kuang.pojo.Paper;
import com.kuang.pojo.Qquestion;
import com.kuang.service.ExamService;
import com.kuang.service.PaperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Controller
@RequestMapping("/paper")
public class PaperController {

    @Autowired
    @Qualifier("PaperServiceImpl")
    private PaperService paperService;

    @Autowired
    @Qualifier("ExamServiceImpl")
    private ExamService examService;


    //出考卷
    @RequestMapping("/start")
    public String startPaper(HttpSession session, Model model){
        Object o=session.getAttribute("EID");
        int eid=Integer.parseInt(o==null?"":o.toString());
        System.out.println("start=>"+eid);
        int order=new Random().nextInt( 6);
        paperService.addPaper(eid,order);
        List<Paper> list=paperService.allPaper(eid);
        model.addAttribute("list", list);
        String msg="开始考试";
        Date beginTime = new Date();
//        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        session.setAttribute("beginTime",beginTime);
        return "kaoshi";
    }
    //完善考卷，得出成绩
    @RequestMapping("/end")
    public String endPaper(HttpSession session, Model model,String[] select_ans){
        Object o=session.getAttribute("EID");
        int eid=Integer.parseInt(o==null?"":o.toString());
        System.out.println("end=>"+eid);
        Date beginTime= (Date) session.getAttribute("beginTime");
        Date endTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String begintime=formatter.format(beginTime);
        String endtime=formatter.format(endTime);
        System.out.println("begintime=>"+begintime+"endtime=>"+endtime);
        int i=0;
        for(String a :select_ans){
            i++;
            System.out.println(a);
            paperService.updatePaper(eid,i,a,begintime,endtime);
        }

        int grade=paperService.getGrade(eid);
        System.out.println("分数为"+grade);

        //更新状态
        examService.updateExam("Y",eid,grade*10);
        return "redirect:/paper/wd";
    }

    //我的
    @RequestMapping("/wd")
    public String getPaper(HttpSession session, Model model){
        Object o=session.getAttribute("EID");
        int eid=Integer.parseInt(o==null?"":o.toString());
        List<Paper> plist=paperService.allPaper(eid);
        int grade=paperService.getGrade(eid)*10;
        model.addAttribute("plist",plist);
        model.addAttribute("start_time",plist.get(0).getSTART_TIME());
        model.addAttribute("end_time",plist.get(0).getEND_TIME());
        model.addAttribute("grade",grade);
        return "wode";
    }


}
